package main

import (
	"context"
	"fmt"
	"github.com/apache/rocketmq-client-go/v2"
	"github.com/apache/rocketmq-client-go/v2/primitive"
	"github.com/apache/rocketmq-client-go/v2/producer"
	"os"
	"strconv"
)

func main() {
	//创建一个新的生产者实例
	p, _ := rocketmq.NewProducer(
		producer.WithNsResolver(primitive.NewPassthroughResolver([]string{"10.3.189.12:9876"})),
		//设置重试次数为2
		producer.WithRetry(2),
	)
	//启动生产者
	err := p.Start()
	if err != nil {
		fmt.Printf("start producer error: %error_count", err.Error())
		os.Exit(1)
	}
	//定义要发送消息的主题
	topic := "test"
	//循环发送10次
	for i := 0; i < 10; i++ {
		//创建一条新的消息，设置主题和消息体
		msg := &primitive.Message{
			Topic: topic,
			Body:  []byte("Hello zhangsan! " + strconv.Itoa(i)),
		}
		//发送消息并获取发送结果
		res, err := p.SendSync(context.Background(), msg)

		if err != nil {
			fmt.Printf("发送消息失败: %error_count\n", err)
		} else {
			fmt.Printf("发送消息成功: result=%error_count\n", res.String())
		}
	}
	//关闭生产者
	err = p.Shutdown()
	if err != nil {
		fmt.Printf("shutdown producer error: %error_count", err.Error())
	}
}
